package com.gem.controller.mobile;

import com.gem.domain.TrainingOrder;
import com.gem.domain.User;
import com.gem.service.TrainingOrderService;
import com.gem.service.UserService;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Bravea❤ ·勇
 * @Description:
 * @create
 * @Modified By:
 */
@Controller
@RequestMapping("/mTraining")
public class MobileTrainingOrderController {

    @Autowired
    private TrainingOrderService trainingOrderService;

    @Autowired
    private UserService userService;

    @Autowired
    private JSONResultUtil result;

    @RequestMapping("/list")
    public String list(Model model, HttpSession session,
                       @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        User user = (User) session.getAttribute("user");
        if (user == null) {
            MessageUtil.returnInfoPage("3", "/starter", "用户未登录", model);
            return "message";
        }
        PageInfo<TrainingOrder> pageBean = trainingOrderService.selectMyTrainingOrderByPage(currentPage,pageSize,user.getPersonname());
        if (currentPage > pageBean.getPages()){
            currentPage = pageBean.getPages();//最后一页被删光了回到上一页
            pageBean = trainingOrderService.selectMyTrainingOrderByPage(currentPage,pageSize,user.getPersonname());
        }
        if(pageBean != null && pageBean.getList().size() > 0 && pageBean.getPages() > 0 && pageBean.getTotal() > 0){
            List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(),currentPage);
            model.addAttribute("pageValue",pageValue);
        }else {
            model.addAttribute("info","没有订单数据请先添加！");
        }
        model.addAttribute("pageBean", pageBean);
        return "mobile_training/TrainingOrder";
    }

    @RequestMapping("/modifyUI")
    public String trainingOrderModifyUI(String trainingOrderId,Model model,String searchUrl,
                                        @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                        @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        if(trainingOrderId == null || trainingOrderId.trim().equals("")){
            MessageUtil.returnInfoPage("2","/mTraining/list?currentPage="+currentPage+"&pageSize="+pageSize,"传递参数为空",model);
            return "mobile_message";
        }
        TrainingOrder trains = trainingOrderService.selectTrainingOrderById(trainingOrderId);
        if (trains == null){
            MessageUtil.returnInfoPage("2","/mTraining/list?currentPage="+currentPage+"&pageSize="+pageSize,"没有该订单",model);
            return "mobile_message";
        }
        List<User> mmembers = userService.findMmembersWithNull();
        List<User> users = userService.findAllUser();
        if(mmembers==null || mmembers.size() <= 0 || users == null || users.size() <= 0){
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("2",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"没有市场人员，请先添加",model);
            }else {
                MessageUtil.returnInfoPage("2","/mTraining/list?currentPage="+currentPage+"&pageSize="+pageSize,"没有市场人员，请先添加",model);
            }
            return "mobile_message";
        }
        model.addAttribute("users",users);
        model.addAttribute("mmembers",mmembers);
        model.addAttribute("searchUrl",searchUrl);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("trains",trains);
        return "mobile_training/TrainingOrder_update";
    }

    @RequestMapping("/modify")
    public String trainingOrderModify(TrainingOrder tor,Model model,String searchUrl,
                                      @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                      @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        boolean isModifySuccess = trainingOrderService.modifyTrainingOrderById(tor);
        if(isModifySuccess){
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("1",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"修改成功！",model);
            }else {
                MessageUtil.returnInfoPage("1", "/mTraining/list?currentPage=" + currentPage + "&pageSize=" + pageSize, "修改成功！", model);
            }
        }else {
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("2",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"修改未成功，请检查！",model);
            }else {
                MessageUtil.returnInfoPage("2", "/mTraining/list?currentPage=" + currentPage + "&pageSize=" + pageSize, "修改未成功，请检查！", model);
            }
        }
        return "mobile_message";
    }

    @RequestMapping("/addUI")
    public String trainingOrderAddUI(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,String searchUrl,
                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,Model model){
        List<User> mmembers = userService.findMmembersWithNull();
        List<User> users = userService.findAllUser();
        if(mmembers == null || mmembers.size() <= 0 || users == null || users.size() <= 0){
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("2",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"没有员工，请先添加",model);
            }else {
                MessageUtil.returnInfoPage("2","/mTraining/list?currentPage="+currentPage+"&pageSize="+pageSize,"没有员工，请先添加",model);
            }
            return "mobile_message";
        }
        model.addAttribute("users",users);
        model.addAttribute("mmembers",mmembers);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("searchUrl",searchUrl);
        return "mobile_training/TrainingOrder_insert";
    }

    @RequestMapping("/add")
    public String trainingOrderAdd(TrainingOrder t,Model model,String searchUrl,
                                   @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                   @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        boolean isAddSuccess = trainingOrderService.addTrainingOrder(t);
        if(isAddSuccess){
            if(searchUrl != null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("1",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"添加成功！",model);
            }else {
                MessageUtil.returnInfoPage("1","/mTraining/list?currentPage="+currentPage+"&pageSize="+pageSize,"添加成功！",model);
            }
        }else {
            if(searchUrl !=null && !searchUrl.trim().equals("")){
                MessageUtil.returnInfoPage("2",searchUrl+"&currentPage="+currentPage+"&pageSize="+pageSize,"添加失败，请检查！",model);
            }else {
                MessageUtil.returnInfoPage("2","/mTraining/list?currentPage="+currentPage+"&pageSize="+pageSize,"添加失败，请检查！",model);
            }
        }
        return "mobile_message";
    }

    @RequestMapping("/search")
    public String trainingOrderSearch(Model model,String trainingOrderPayDate,TrainingOrder o,HttpSession session,
                                      @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                      @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        User user = (User) session.getAttribute("user");
        if (user == null) {
            MessageUtil.returnInfoPage("2", "/starter", "用户未登录", model);
            return "mobile_message";
        }
        if (trainingOrderPayDate == null){
            trainingOrderPayDate = "";
        }
        if (o.getTrainingOrderIDNumber() == null){
            o.setTrainingOrderIDNumber("");
        }
        if (o.getTrainingOrderName() == null){
            o.setTrainingOrderName("");
        }
        PageInfo<TrainingOrder> pageBean = null;
        if(user.getDepartment() != null && user.getDepartment().equals("市场") && user.getAuthority() == 1){
            pageBean = trainingOrderService.searchTrainingOrderByPageNoOther(currentPage,pageSize,o.getTrainingOrderName(),o.getTrainingOrderIDNumber(),o.getTrainingOrderPayDate());
        }else if (user.getDepartment() != null && user.getDepartment().equals("财务") && user.getAuthority() == 1){
            pageBean = trainingOrderService.searchTrainingOrderByPage(currentPage,pageSize,o.getTrainingOrderName(),o.getTrainingOrderIDNumber(),o.getTrainingOrderPayDate());
        }else {
            pageBean = trainingOrderService.searchMyTrainingOrderByPage(currentPage,pageSize,o.getTrainingOrderName(),o.getTrainingOrderIDNumber(),o.getTrainingOrderPayDate(),user.getPersonname());
        }
        if (currentPage > pageBean.getPages()){
            currentPage = pageBean.getPages();//最后一页被删光了回到上一页
            if(user.getDepartment() != null && user.getDepartment().equals("市场") && user.getAuthority() == 1){
                pageBean = trainingOrderService.searchTrainingOrderByPageNoOther(currentPage,pageSize,o.getTrainingOrderName(),o.getTrainingOrderIDNumber(),o.getTrainingOrderPayDate());
            }else if (user.getDepartment() != null && user.getDepartment().equals("财务") && user.getAuthority() == 1){
                pageBean = trainingOrderService.searchTrainingOrderByPage(currentPage,pageSize,o.getTrainingOrderName(),o.getTrainingOrderIDNumber(),o.getTrainingOrderPayDate());
            }else {
                pageBean = trainingOrderService.searchMyTrainingOrderByPage(currentPage,pageSize,o.getTrainingOrderName(),o.getTrainingOrderIDNumber(),o.getTrainingOrderPayDate(),user.getPersonname());
            }
        }
        if(pageBean != null && pageBean.getList().size() > 0 && pageBean.getPages() > 0 && pageBean.getTotal() > 0){
            List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(),currentPage);
            model.addAttribute("pageValue",pageValue);
        }else {
            model.addAttribute("info","没有订单数据请先添加！");
        }
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("trainingOrderName",o.getTrainingOrderName());
        model.addAttribute("trainingOrderIDNumber",o.getTrainingOrderIDNumber());
        model.addAttribute("trainingOrderPayDate",trainingOrderPayDate);
        model.addAttribute("searchUrl","/mTraining/search?trainingOrderName="+o.getTrainingOrderName()+"&trainingOrderIDNumber="+o.getTrainingOrderIDNumber()+"&trainingOrderPayDate="+trainingOrderPayDate);
        return "mobile_training/TrainingOrder";
    }
    //单个删除
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil crDelete(String[] checks) {
        try {
            if (checks != null && checks.length>0) {
                if (trainingOrderService.deleteTrainingOrderById(checks)) {
                    result.setId(0);
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("服务器异常，删除失败哦！");
                }
            } else {
                result.setId(-1);
                result.setData("服务器异常，没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }


}
